package com.springboot.cloud.demos.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.common.web.redis.CacheDuration;
import com.springboot.cloud.common.web.redis.ExpireTimeConstant;
import com.springboot.cloud.demos.entity.form.UserQueryForm;
import com.springboot.cloud.demos.entity.param.UserQueryParam;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.producer.dao.UserMapper;
import com.springboot.cloud.demos.producer.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@CacheDuration(duration = ExpireTimeConstant.ONE_DAY)
@RefreshScope
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Value("${producer.user:123}")
    private String value;

    @Override
    @CacheEvict(value = "user", allEntries = true)
    public boolean add(User user) {
        return this.save(user);
    }

    @Override
    @CacheEvict(value = "user", allEntries = true)
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @Cacheable(value = "user", key = "#root.targetClass+'-'+#mobile")
    public User queryByMobile(String mobile) {
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("mobile", mobile);
        List<User> users = this.list(queryWrapper);
        return users.isEmpty() ? null : users.get(0);
    }

    @Override
    public Map<String, Object> getStatistics(String userId) {
        Integer followMeCount = this.getBaseMapper().getFollowMeCount(userId);
        Integer myFollowCount = this.getBaseMapper().getMyFollowCount(userId);
        Map map = new HashMap();
        map.put("followMe", followMeCount);
        map.put("myFollow", myFollowCount);
        return map;
    }

    @Override
    public IPage page(UserQueryForm userQueryForm) {
        UserQueryParam userQueryParam = userQueryForm.toParam(UserQueryParam.class);
        Page<User> page = new Page<>(userQueryForm.getCurrent(), userQueryForm.getSize());
        QueryWrapper<User> queryWrapper = userQueryParam.build();
        if (StringUtils.isNotEmpty(userQueryParam.getMobile())) {
            queryWrapper.eq("mobile", userQueryParam.getMobile());
        }

        if (StringUtils.isNotEmpty(userQueryParam.getOpenId())) {
            queryWrapper.eq("open_id", userQueryParam.getOpenId());
        }
        if (StringUtils.isNotEmpty(userQueryParam.getUnionId())) {
            queryWrapper.eq("union_id", userQueryParam.getUnionId());
        }

        IPage<User> iPage = this.getBaseMapper().selectPage(page, queryWrapper);
        return iPage;
    }

    @Override
    public IPage nearBy(UserQueryForm userQueryForm) {
        UserQueryParam userQueryParam = userQueryForm.toParam(UserQueryParam.class);
        Page<User> page = new Page<>(userQueryForm.getCurrent(), userQueryForm.getSize());
        QueryWrapper<User> queryWrapper = userQueryParam.build();
        if (StringUtils.isNotEmpty(userQueryParam.getMobile())) {
            queryWrapper.eq("mobile", userQueryParam.getMobile());
        }

        if (StringUtils.isNotEmpty(userQueryParam.getOpenId())) {
            queryWrapper.eq("open_id", userQueryParam.getOpenId());
        }
        if (StringUtils.isNotEmpty(userQueryParam.getUserId())) {
            queryWrapper.ne("a.id", userQueryParam.getUserId());
        }
        queryWrapper.isNotNull("b.latitude");
        IPage<Map> iPage = this.getBaseMapper().nearBy(page, userQueryForm.getLatitude(),userQueryForm.getLongitude(),queryWrapper);
        return iPage;
    }

    @Override
    public IPage recommend(UserQueryForm userQueryForm) {
        UserQueryParam userQueryParam = userQueryForm.toParam(UserQueryParam.class);
        Page<User> page = new Page<>(userQueryForm.getCurrent(), userQueryForm.getSize());
        QueryWrapper<User> queryWrapper = userQueryParam.build();
        if (StringUtils.isNotEmpty(userQueryParam.getMobile())) {
            queryWrapper.eq("mobile", userQueryParam.getMobile());
        }

        if (StringUtils.isNotEmpty(userQueryParam.getOpenId())) {
            queryWrapper.eq("open_id", userQueryParam.getOpenId());
        }
        if (StringUtils.isNotEmpty(userQueryParam.getUnionId())) {
            queryWrapper.eq("union_id", userQueryParam.getUnionId());
        }

        IPage<User> iPage = this.getBaseMapper().selectPage(page, queryWrapper);
        return iPage;
    }

    @Override
    public IPage getFollowMe(UserQueryForm userQueryForm) {
        UserQueryParam userQueryParam = userQueryForm.toParam(UserQueryParam.class);
        Page<User> page = new Page<>(userQueryForm.getCurrent(), userQueryForm.getSize());
        QueryWrapper<User> queryWrapper = userQueryParam.build();
        if (StringUtils.isNotEmpty(userQueryParam.getFollowUserId())) {
            queryWrapper.eq("a.follow_user_id", userQueryParam.getFollowUserId());
        }
        log.info("queryWrapper:{}",queryWrapper);
        IPage<User> iPage = this.getBaseMapper().getFollowMe(page, userQueryParam.getFollowUserId(), queryWrapper);
        log.info("iPage:{}",iPage);
        return iPage;
    }

    @Override
    @Cacheable(value = "user", key = "#root.targetClass+'-getMyFollow-'+#userQueryForm.userId+'-'+#userQueryForm.current")
    public IPage getMyFollow(UserQueryForm userQueryForm) {
        UserQueryParam userQueryParam = userQueryForm.toParam(UserQueryParam.class);
        Page<User> page = new Page<>(userQueryForm.getCurrent(), userQueryForm.getSize());
        QueryWrapper<User> queryWrapper = userQueryParam.build();
        if (StringUtils.isNotEmpty(userQueryParam.getUserId())) {
            queryWrapper.eq("a.user_id", userQueryParam.getUserId());
        }

        IPage<User> iPage = this.getBaseMapper().getMyFollow(page, userQueryParam.getUserId(), queryWrapper);
        return iPage;
    }

    @Override
    @CacheEvict(value = "user", allEntries = true)
    public boolean update(User user) {
        return this.updateById(user);
    }

    @Override
    @Cacheable(value = "user", key = "#root.targetClass+'-'+#id")
    public User get(String id) {
        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
    public List<User> query(UserQueryParam userQueryParam) {
        QueryWrapper<User> queryWrapper = userQueryParam.build();
        if (StringUtils.isNotEmpty(userQueryParam.getMobile())) {
            queryWrapper.eq("mobile", userQueryParam.getMobile());
        }

        if (StringUtils.isNotEmpty(userQueryParam.getOpenId())) {
            queryWrapper.eq("open_id", userQueryParam.getOpenId());
        }
        if (StringUtils.isNotEmpty(userQueryParam.getUnionId())) {
            queryWrapper.eq("union_id", userQueryParam.getUnionId());
        }

        queryWrapper.orderByDesc("created_time");
        return this.list(queryWrapper);
    }
}
